package com.winai.launcher.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.interfaces.OnAiyishengVersionCallbackListener;
import com.winai.launcher.manager.AiyishengManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class VersionUtils {

    private static final String FILE_NAME = "/K_ALIAS_KEY.txt";//配置测试分支的文件，路径：/storage/emulated/0

    /**
     * @date 2020/10/9
     * @desc 获取艾益生版本号
     */
    public static void getAiyishengVersionName(Context context) {
        AiyishengManager.getInstance(context).getAiyishengVisionName(new OnAiyishengVersionCallbackListener() {
            @Override
            public void onSuccess(String version) {
                DB_Common dbCommon = new DB_Common(context);
                dbCommon.add("aiyishengVersion", version);
            }

            @Override
            public void onError() {

            }
        });
    }

    /**
     * @date 2020/10/12
     * @desc 获取艾益生版本号
     */
    public static String getAiyishengCode(Context context) {
        DB_Common dbCommon = new DB_Common(context);
        String aiyishengVersion = dbCommon.get("aiyishengVersion");
        boolean empty = TextUtils.isEmpty(aiyishengVersion);
        if (empty) {
            return "unknown";
        } else {
            return aiyishengVersion.substring(0, 5);
        }
    }

    /**
     * @date 2020/10/9
     * @desc 获取艾益生版本发布日期
     */
    public static int getAiyishengDate(Context context) {
        DB_Common dbCommon = new DB_Common(context);
        String aiyishengVersion = dbCommon.get("aiyishengVersion");
        boolean empty = TextUtils.isEmpty(aiyishengVersion);
        if (empty) {
            return 0;
        } else {
            return Integer.parseInt(aiyishengVersion.substring(6, 14));
        }
    }

    /**
     * @date 2020/10/12
     * @desc 获取艾益生版本号
     */
    public static String getAiyishengEnd(Context context) {
        DB_Common dbCommon = new DB_Common(context);
        String aiyishengVersion = dbCommon.get("aiyishengVersion");
        boolean empty = TextUtils.isEmpty(aiyishengVersion);
        if (empty) {
            return "unknown";
        } else {
            return aiyishengVersion.substring(0, 5);
        }
    }

    /**
     * @date 2020/10/9
     * @desc 获取系统版本发布日期
     */
    public static int getSystemDate() {
        String systemVersion = BaseUtils.getSystemVersion();
        return Integer.parseInt(systemVersion.substring(6, 14));
    }

    /**
     * @date 2020/10/9
     * @desc 获取系统版本号
     */
    public static String getSystemVersion() {
        return BaseUtils.getSystemVersion();
    }

    /**
     * @date 2020/10/9
     * @desc 获取launcher版本发布日期
     */
    public static int getLauncherDate() {
        return getSystemDate();
    }

    /**
     * @date 2020/10/9
     * @desc 获取系统版本号
     */
    public static String getLauncherVersion(Context context) {
        return BaseUtils.getLauncherVersion(context) + "." + getLauncherDate() + getEnd();
    }

    /**
     * @date 2020/10/10
     * @desc 获取ai的内核版本
     */
    public static String getAiVersion(Context context) {
        DB_Common dbCommon = new DB_Common(context);
        String aiVersion = dbCommon.get("aiVersion");//1.2.10.1
        if (!TextUtils.isEmpty(aiVersion)) {
            if (!TextUtils.isEmpty(read()) && !read().equals("Release")) {
                return aiVersion + "." + getSystemDate() + "_" + read();
            } else {
                return aiVersion + "." + getSystemDate();
            }
        } else {
            return null;
        }
    }

    /**
     * @desc 测试读取分支配置文件
     */
    private static String read() {
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //获得SD卡对应的存储目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                //获取指定文件对应的输入流
                String name = sdCardDir.getCanonicalPath() + FILE_NAME;//路径：/storage/emulated/0
                LogUtils.e("语音配置分支的文件位置：" + name);
                FileInputStream fis = new FileInputStream(name);
                //将指定输入流包装成BufferReader
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                StringBuilder sb = new StringBuilder("");
                String line;
                //循环读取文件内容
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @date 2020/10/10
     * @desc 获取最新的日期
     */
    public static String getNewestDate(Context context) {
        final int[] newestDate = {0};
        if (newestDate[0] < getAiyishengDate(context)) {
            newestDate[0] = getAiyishengDate(context);
        }
        if (newestDate[0] < getSystemDate()) {
            newestDate[0] = getSystemDate();
        }
        return String.valueOf(newestDate[0]);
    }

    /**
     * @date 2020/10/12
     * @desc 获取版本后缀
     */
    public static String getEnd() {
        String systemVersion = getSystemVersion();
        String regex = "_";
        String[] split = systemVersion.split(regex);
        if (split.length == 2) {
            if (split[1].equals("Release")) {
                return "";
            } else {
                return "_" + split[1];
            }
        } else {
            return "";
        }
    }

    /**
     * @date 2020/10/9
     * @desc 获取最新的版本号
     */
    public static String getNewestVersionCode(Context context) {
        return getAiyishengCode(context) + "." + getNewestDate(context) + getEnd();
    }
}
